# 1. 类的定义
# class Myclass:
#     num = 100 # 类属性，也是类成员变量
#     def __init__(self, name, gender): # 构造函数
#         self.name = name # 对象成员变量
#         self.gender = gender # 对象成员变量
#
#     def disp(self): # 类成员方法(类中函数带有self). 方法中第一个参数为self表示该方法为类成员方法
#         print(f"num = {self.num}")
#         print(f"性别是{self.name},性别是{self.gender}")
#
#     def __del__(self): # 析构函数
#         print(f"{self}对象已销毁")
#
# my = Myclass('张三丰', '男') # 对象实例化
# my.disp() # 调用方法
# lisi = Myclass("李四","女")
# lisi.disp()

# 2. 一个类中同时有多个构造函数是否可行
# class Person:
#     sno='1001' # 类成员变量
#     # def __init__(self): # 注意: 构造函数有且只能有一个
#     #     print("我是无参的构造函数")
#
#     def __init__(self,n,gender,birthday): # 有参构造函数
#         self.name = n
#         self.gender = gender
#         self.bir = birthday
#     def mydisp(self): # 成员函数
#         print(self.sno)
#     def myprint(): # 普通方法
#         print(f"我是普通方法{Person.sno}") # 普通方法访问类成员需要使用类名.xx的格式
#     def sayhello(self): #
#         print(f"我的名字是{self.name}, 性别是{self.gender}")
#
# # p1 = Person()
# # p1.mydisp()
#
# p2 = Person("张三丰","男", "1990-11-1")
# p2.sayhello()
# p2.mydisp()
# # 普通方法在类外的使用 类名.方法
# Person.myprint()

# 3. 私有成员访问
# """
# 私有成员，变量名前加两个下划线
# 类外访问私有成员的方法
# 1. 类名._类名__私有成员
# 2. 对象._类名__私有成员
# """
# class Car(object):
#     salesPrice = 150000 # 公有成员
#     __manufacturePrice = 120000 # 私有成员
#     def __init__(self, brand, serial):
#         self.brand = brand
#         self.__serial = serial
#
# print(f"访问类的公有属性salesPrice:{Car.salesPrice}")
# print(f"访问类的私有属性salesPrice:{Car._Car__manufacturePrice}")
# c = Car('大众', '一汽高尔夫')
# print('访问对象c的公有属性brand:', c.brand)
# print('访问对象c的私有属性serial:', c._Car__serial)

# 4. 类方法操作
# class DemoClass2:
#     def __init__(self, year = 0, month = 0, day = 0):
#         self.day = day
#         self.month = month
#         self.year = year
#     @classmethod
#     def get_date(cls, string_date):
#         # 第一个参数cls，表示调用当前的类名
#         year,month,day = map(int, string_date.split('-'))
#         date1 = cls(year, month, day)
#         return date1
#     def output_date(self):
#         print(f"year:{self.year}, month:{self.month}, day:{self.day}")
#
# rq1 = DemoClass2(2024,10,21)
# rq1.output_date()
# rq2 = DemoClass2.get_date("2024-10-21")
# rq2.output_date()

# 5. 类的继承
# class Animal:
#     num = 0
#     def __init__(self):
#         print("父类Animal")
#     def show(self):
#         print("父类Animal成员方法")
#
# class Cat(Animal):
#     def __init__(self):
#         print("构建子类Cat")
#
#     def run(self):
#         print("子类Cat成员方法")
#
# cat = Cat()
# cat.run()
# cat.show()

#6. 私有属性、私有方法的操作
# class Employee:
#     def __init__(self, name="", department="computer", age=20):
#         self.setName(name)
#         self.setDepartment(department)
#         self.setAge(age)
# 
#     def setName(self, name):
#         if type(name) != str:
#             print("姓名必须是字符")
#             return
#         self.__name = name
# 
#     def setDepartment(self, department):
#         if department not in ["computer", "communication", "electric"]:
#             print("专业必须是computer、communication或electric")
#             return
#         self.__department = department
# 
#     def setAge(self, age):
#         if type(age) != int or age >= 33 or age < 20:
#             print("年龄必须是数字，且界于20至33之间")
#             return
#         self.__age = age
# 
#     def show(self):
#         print("姓名：{} 专业：{} 年龄：{}".
#               format(self.__name, self.__department, self.__age))
# 
# 
# class ProjectManager(Employee):
#     def __init__(self, name='', department="computer", age=22, title="middle"):
#         Employee.__init__(self, name, department, age)
#         self.setTitle(title)
# 
#     def setTitle(self, title):
#         self.__title = title
# 
#     def show(self):
#         Employee.show(self)
#         print("职称：{}".format(self.__title))
# 
# 
# try:
#     emp1 = Employee("Rose")
#     emp1.show()
#     pm1 = ProjectManager("Mike", "electric", 26, "high")
#     pm1.setAge(30)
#     pm1.show()
# except Exception as ex:
#     print("数据出现错误", ex)
# 
# 
# class Employee:
#     def __init__(self, name="", department="computer", age=20):
#         self.setName(name)
#         self.setDepartment(department)
#         self.setAge(age)
# 
#     def setName(self, name):
#         if type(name) != str:
#             print("姓名必须是字符")
#             return
#         self.__name = name
# 
#     def setDepartment(self, department):
#         if department not in ["computer", "communication", "electric"]:
#             print("专业必须是computer、communication或electric")
#             return
#         self.__department = department
# 
#     def setAge(self, age):
#         if type(age) != int or age >= 33 or age < 20:
#             print("年龄必须是数字，且界于20至33之间")
#             return
#         self.__age = age
# 
#     def show(self):
#         print("姓名：{} 专业：{} 年龄：{}".
#               format(self.__name, self.__department, self.__age))
# 
# 
# class ProjectManager(Employee):
#     def __init__(self, name='', department="computer", age=22, title="middle"):
#         Employee.__init__(self, name, department, age)
#         self.setTitle(title)
# 
#     def setTitle(self, title):
#         self.__title = title
# 
#     def show(self):
#         Employee.show(self)
#         print("职称：{}".format(self.__title))
# 
# try:
#     emp1 = Employee("Rose")
#     emp1.show()
#     pm1 = ProjectManager("Mike", "electric", 26, "high")
#     pm1.setAge(30)
#     pm1.show()
# except Exception as ex:
#     print("数据出现错误", ex)

# 7. 多态操作
# class Animal:
#     def __init__(self,aname):
#         self.name=aname
#     def enjoy(self):
#         print("nangnang")
# class Cat(Animal):
#     def enjoy(self):
#         print(self.name," niaoniao")
# class Dog(Animal):
#     def enjoy(self):
#         print(self.name+" wangwang")
# class Person:
#     def __init__(self,id,name):
#         self.name=name
#         self.id=id
#     def drive(self,ani):
#         ani.enjoy()
# cat=Cat("Mikey")
# dog=Dog("Dahuang")
# person=Person("zhang3",9)
# #同一对象调用同一样方法，传递不同参数，得到结果不同。
# person.drive(cat)  #调用drive方法
# person.drive(dog) #调用drive方法
